package com.example.composestudy.ui.home

import android.R.attr.content
import androidx.compose.animation.animateContentSize
import androidx.compose.animation.core.Spring
import androidx.compose.animation.core.spring
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.KeyboardArrowDown
import androidx.compose.material.icons.filled.KeyboardArrowUp
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.composestudy.R
import com.example.composestudy.data.models.HomeData
import com.example.composestudy.data.models.HomeObject
import com.example.composestudy.ui.theme.MyApplicationTheme
import com.example.composestudy.ui.widget.MyTopAppBar

@Composable
fun ListPage(
    data: List<HomeObject>,
    modifier: Modifier = Modifier,
    navTitle: String = "",
    showBackIcon: Boolean = true,
    onBack: () -> Unit,
    onClick: (route: String) -> Unit,
) {
    Scaffold(topBar = {
        MyTopAppBar(title = navTitle, showBackIcon = showBackIcon) {
            onBack.invoke()
        }
    }) { innerPadding ->
        LazyColumn(
            modifier = modifier
                .padding(vertical = 4.dp)
                .padding(top = innerPadding.calculateTopPadding())
        ) {
            items(items = data) { homeObject ->
                CardItem(homeObject, onClick = onClick)
            }
        }
    }
}

@Composable
fun CardItem(
    homeObject: HomeObject,
    modifier: Modifier = Modifier,
    onClick: (route: String) -> Unit
) {
    Card(
        colors = CardDefaults.cardColors(
            containerColor = MaterialTheme.colorScheme.secondaryContainer
        ), modifier = modifier
            .padding(horizontal = 8.dp, vertical = 4.dp)
            .clickable {
                onClick(homeObject.route)
            }
    ) {
        CardContent(homeObject)
    }
}

@Composable
private fun CardContent(homeObject: HomeObject) {
    var expanded by rememberSaveable { mutableStateOf(true) }

    Row(
        modifier = Modifier
            .padding(1.dp)
            .animateContentSize(
                animationSpec = spring(
                    dampingRatio = Spring.DampingRatioLowBouncy,
                    stiffness = Spring.StiffnessLow
                )
            )
    ) {
        Column(
            modifier = Modifier
                .weight(1f)
                .padding(8.dp)
        ) {
            Text(text = homeObject.title)
            if (expanded) {
                Text(
                    text = homeObject.description,
                    style = TextStyle(color = MaterialTheme.colorScheme.secondary, fontSize = 14.sp)
                )
            }
        }
        IconButton(onClick = { expanded = !expanded }) {
            Icon(
                imageVector = if (expanded) Icons.Filled.KeyboardArrowUp else Icons.Filled.KeyboardArrowDown,
                contentDescription = if (expanded) {
                    stringResource(R.string.show_less)
                } else {
                    stringResource(R.string.show_more)
                }
            )
        }
    }
}


@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    MyApplicationTheme {
        ListPage(data = HomeData.conversationHomeObject, onClick = {}, onBack = {})
    }
}